<!DOCTYPE html>
<html>
<head>
  <title>HttpClient.java</title>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
  <link rel='stylesheet' type='text/css' href='../../../../../../coverage.css'/>
  <link rel='shortcut icon' type='image/png' href='../../../../../../logo.png'/>
  <script type='text/javascript' src='../../../../../../coverage.js'></script>
  <script type='text/javascript' src='../../../../../../prettify.js'></script>
</head>
<body onload='prettyPrint()'>
  <table cellpadding='0' cellspacing='1'>
    <caption>igraph_client_java/src/main/java/com/taobao/igraph/vipserver/client/net/HttpClient.java</caption>
    <tr>
      <td class='line'>1</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>package com.taobao.igraph.vipserver.client.net;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'></td><td>&nbsp;</td>
      <td><pre class='imports prettyprint' onclick='showHideLines(this)'><div>import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.commons.lang.StringUtils;

import com.taobao.vipserver.client.utils.IOUtils;
</div><span>import ...</span></pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>20</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>public class HttpClient {</pre></td>
    </tr>
    <tr>
      <td class='line'>21</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public static final int TIME_OUT_MILLIS = 1000;</pre></td>
    </tr>
    <tr>
      <td class='line'>22</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public static final int CON_TIME_OUT_MILLIS = 200;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>24</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    static {</pre></td>
    </tr>
    <tr>
      <td class='line'>25</td><td>&nbsp;</td>
      <td><pre class='comment'>        // limit max redirection</pre></td>
    </tr>
    <tr>
      <td class='line'>26</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        System.setProperty("http.maxRedirects", "5");</pre></td>
    </tr>
    <tr>
      <td class='line'>27</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>29</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public static HttpResult httpGet(String url, List&lt;String> headers, Map&lt;String, String> paramValues, String encoding) {</pre></td>
    </tr>
    <tr>
      <td class='line'>30</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        HttpURLConnection conn = null;</pre></td>
    </tr>
    <tr>
      <td class='line'>31</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        try {</pre></td>
    </tr>
    <tr>
      <td class='line'>32</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            String encodedContent = encodingParams(paramValues, encoding);</pre></td>
    </tr>
    <tr>
      <td class='line'>33</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            url += (null == encodedContent) ? "" : ("?" + encodedContent);</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>35</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            conn = (HttpURLConnection) new URL(url).openConnection();</pre></td>
    </tr>
    <tr>
      <td class='line'>36</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            conn.setConnectTimeout(CON_TIME_OUT_MILLIS);</pre></td>
    </tr>
    <tr>
      <td class='line'>37</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            conn.setReadTimeout(TIME_OUT_MILLIS);</pre></td>
    </tr>
    <tr>
      <td class='line'>38</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            conn.setRequestMethod("GET");</pre></td>
    </tr>
    <tr>
      <td class='line'>39</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            setHeaders(conn, headers, encoding);</pre></td>
    </tr>
    <tr>
      <td class='line'>40</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            conn.connect();</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>42</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            return getResult(conn);</pre></td>
    </tr>
    <tr>
      <td class='line'>43</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        } catch (Exception e) {</pre></td>
    </tr>
    <tr>
      <td class='line'>44</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            return new HttpResult(500, e.toString(), Collections.&lt;String, String>emptyMap());</pre></td>
    </tr>
    <tr>
      <td class='line'>45</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        } finally {</pre></td>
    </tr>
    <tr>
      <td class='line'>46</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            if (conn != null) {</pre></td>
    </tr>
    <tr>
      <td class='line'>47</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                conn.disconnect();</pre></td>
    </tr>
    <tr>
      <td class='line'>48</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            }</pre></td>
    </tr>
    <tr>
      <td class='line'>49</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr>
      <td class='line'>50</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>52</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    private static HttpResult getResult(HttpURLConnection conn) throws IOException {</pre></td>
    </tr>
    <tr>
      <td class='line'>53</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        int respCode = conn.getResponseCode(); // �����ڲ���������</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>55</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        InputStream inputStream;</pre></td>
    </tr>
    <tr>
      <td class='line'>56</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        if (HttpURLConnection.HTTP_OK == respCode</pre></td>
    </tr>
    <tr>
      <td class='line'>57</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                || HttpURLConnection.HTTP_NOT_MODIFIED == respCode) {</pre></td>
    </tr>
    <tr>
      <td class='line'>58</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            inputStream = conn.getInputStream();</pre></td>
    </tr>
    <tr>
      <td class='line'>59</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        } else {</pre></td>
    </tr>
    <tr>
      <td class='line'>60</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            inputStream = conn.getErrorStream();</pre></td>
    </tr>
    <tr>
      <td class='line'>61</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>63</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        Map&lt;String, String> respHeaders = new HashMap&lt;String, String>(conn.getHeaderFields().size());</pre></td>
    </tr>
    <tr>
      <td class='line'>64</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        for (Map.Entry&lt;String, List&lt;String>> entry : conn.getHeaderFields().entrySet()) {</pre></td>
    </tr>
    <tr>
      <td class='line'>65</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            respHeaders.put(entry.getKey(), entry.getValue().get(0));</pre></td>
    </tr>
    <tr>
      <td class='line'>66</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>68</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        if ("gzip".equals(respHeaders.get("Content-Encoding"))) {</pre></td>
    </tr>
    <tr>
      <td class='line'>69</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            inputStream = new GZIPInputStream(inputStream);</pre></td>
    </tr>
    <tr>
      <td class='line'>70</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>72</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        return new HttpResult(respCode, IOUtils.toString(inputStream, getCharset(conn)), respHeaders);</pre></td>
    </tr>
    <tr>
      <td class='line'>73</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>75</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    private static String getCharset(HttpURLConnection conn) {</pre></td>
    </tr>
    <tr>
      <td class='line'>76</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        String contentType = conn.getContentType();</pre></td>
    </tr>
    <tr>
      <td class='line'>77</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        if (StringUtils.isEmpty(contentType)) {</pre></td>
    </tr>
    <tr>
      <td class='line'>78</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            return "GBK";</pre></td>
    </tr>
    <tr>
      <td class='line'>79</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>81</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        String values[] = contentType.split(";");</pre></td>
    </tr>
    <tr>
      <td class='line'>82</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        if (values.length == 0) {</pre></td>
    </tr>
    <tr>
      <td class='line'>83</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            return "GBK";</pre></td>
    </tr>
    <tr>
      <td class='line'>84</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>86</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        String charset = "GBK";</pre></td>
    </tr>
    <tr>
      <td class='line'>87</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        for (String value : values) {</pre></td>
    </tr>
    <tr>
      <td class='line'>88</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            value = value.trim();</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>90</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            if (value.toLowerCase().startsWith("charset=")) {</pre></td>
    </tr>
    <tr>
      <td class='line'>91</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                charset = value.substring("charset=".length());</pre></td>
    </tr>
    <tr>
      <td class='line'>92</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            }</pre></td>
    </tr>
    <tr>
      <td class='line'>93</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>95</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        return charset;</pre></td>
    </tr>
    <tr>
      <td class='line'>96</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>98</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    private static void setHeaders(HttpURLConnection conn, List&lt;String> headers, String encoding) {</pre></td>
    </tr>
    <tr>
      <td class='line'>99</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        if (null != headers) {</pre></td>
    </tr>
    <tr>
      <td class='line'>100</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            for (Iterator&lt;String> iter = headers.iterator(); iter.hasNext(); ) {</pre></td>
    </tr>
    <tr>
      <td class='line'>101</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                conn.addRequestProperty(iter.next(), iter.next());</pre></td>
    </tr>
    <tr>
      <td class='line'>102</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            }</pre></td>
    </tr>
    <tr>
      <td class='line'>103</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>105</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="</pre></td>
    </tr>
    <tr>
      <td class='line'>106</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                + encoding);</pre></td>
    </tr>
    <tr>
      <td class='line'>107</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        conn.addRequestProperty("Accept-Charset", encoding);</pre></td>
    </tr>
    <tr>
      <td class='line'>108</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>110</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    private static String encodingParams(Map&lt;String, String> params, String encoding)</pre></td>
    </tr>
    <tr>
      <td class='line'>111</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            throws UnsupportedEncodingException {</pre></td>
    </tr>
    <tr>
      <td class='line'>112</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        StringBuilder sb = new StringBuilder();</pre></td>
    </tr>
    <tr>
      <td class='line'>113</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        if (null == params || params.isEmpty()) {</pre></td>
    </tr>
    <tr>
      <td class='line'>114</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            return null;</pre></td>
    </tr>
    <tr>
      <td class='line'>115</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>117</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        params.put("encoding", encoding);</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>119</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        for (Map.Entry&lt;String, String> entry : params.entrySet()) {</pre></td>
    </tr>
    <tr>
      <td class='line'>120</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            if (StringUtils.isEmpty(entry.getValue())) {</pre></td>
    </tr>
    <tr>
      <td class='line'>121</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                continue;</pre></td>
    </tr>
    <tr>
      <td class='line'>122</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>124</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            sb.append(entry.getKey()).append("=");</pre></td>
    </tr>
    <tr>
      <td class='line'>125</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            sb.append(URLEncoder.encode(entry.getValue(), encoding));</pre></td>
    </tr>
    <tr>
      <td class='line'>126</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            sb.append("&");</pre></td>
    </tr>
    <tr>
      <td class='line'>127</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>129</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        return sb.toString();</pre></td>
    </tr>
    <tr>
      <td class='line'>130</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>132</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public static class HttpResult {</pre></td>
    </tr>
    <tr>
      <td class='line'>133</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        final public int code;</pre></td>
    </tr>
    <tr>
      <td class='line'>134</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        final public String content;</pre></td>
    </tr>
    <tr>
      <td class='line'>135</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        final private Map&lt;String, String> respHeaders;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>137</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        public HttpResult(int code, String content, Map&lt;String, String> respHeaders) {</pre></td>
    </tr>
    <tr>
      <td class='line'>138</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            this.code = code;</pre></td>
    </tr>
    <tr>
      <td class='line'>139</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            this.content = content;</pre></td>
    </tr>
    <tr>
      <td class='line'>140</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            this.respHeaders = respHeaders;</pre></td>
    </tr>
    <tr>
      <td class='line'>141</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>143</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        public String getHeader(String name) {</pre></td>
    </tr>
    <tr>
      <td class='line'>144</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            return respHeaders.get(name);</pre></td>
    </tr>
    <tr>
      <td class='line'>145</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr>
      <td class='line'>146</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr>
      <td class='line'>147</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>}</pre></td>
    </tr>
  </table>
</body>
</html>
